﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Data;
using AtletIS.Shared.Helpers;

namespace AtletIS.Shared.Forms.Admin
{
    public partial class frmAdminCompetitors : Form
    {
        private aDbDataSet.CompetitorsRow drActiveCompetitor;
        private bool bNew;
        
        // Constructor
        public frmAdminCompetitors()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.AtletIS64;
        }

        // Load
        private void frmAdminCompetitors_Load(object sender, EventArgs e)
        {
            this.countriesTableAdapter.Fill(this.aDbDataSet.Countries);
            tsbCancel.Enabled = false;
            tsbSave.Enabled = false;

            this.clubsTableAdapter.Fill(this.aDbDataSet.Clubs);
            if (!string.IsNullOrEmpty(clubsComboBox.SelectedValue.ToString()))
            {
                DatabaseHelper.FillCompetitorsByClubId(this.aDbDataSet.Competitors, clubsComboBox.SelectedValue.ToString());
            }
        }

        // Club changed
        private void clubsComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(clubsComboBox.SelectedValue.ToString()))
            {
                DatabaseHelper.FillCompetitorsByClubId(this.aDbDataSet.Competitors, clubsComboBox.SelectedValue.ToString());
            }
        }

        // Selection changed
        private void competitorsDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (competitorsDataGridView.CurrentRow == null)
                return;

            try
            {
                DataRowView drv = (DataRowView)competitorsDataGridView.CurrentRow.DataBoundItem;
                chkActive.Checked = Convert.ToBoolean(drv["Active"]);
                txtSurname.Text = drv["Surname"].ToString();
                txtFirstname.Text = drv["Firstname"].ToString();
                dtpBirthDate.Value = Convert.ToDateTime(drv["Birthdate"]);
                txtSex.Text = drv["Sex"].ToString();
                clubsComboBox1.SelectedValue = drv["ClubId"].ToString();
                countriesComboBox.SelectedValue = drv["CountryId"].ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Kluby", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // New
        private void tsbNew_Click(object sender, EventArgs e)
        {
            try
            {
                bNew = true;
                tsbNew.Enabled = false;
                tsbEdit.Enabled = false;
                clubsComboBox.Enabled = false;
                tsbCancel.Enabled = true;
                tsbSave.Enabled = true;

                chkActive.Checked = true;
                txtFirstname.Text = string.Empty;
                txtSurname.Text = string.Empty;
                dtpBirthDate.Value = new DateTime(1990, 1, 1);
                txtSex.Text = string.Empty;
                clubsComboBox1.SelectedValue = clubsComboBox.SelectedValue;
                countriesComboBox.SelectedValue = "SVK";

                this.drActiveCompetitor = aDbDataSet.Competitors.NewCompetitorsRow();

                this.drActiveCompetitor["Active"] = true;
                this.drActiveCompetitor["Sync"] = false;
                this.drActiveCompetitor["WebId"] = 0;

                gbList.Enabled = false;
                gbDetails.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Pretekári - pridať", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Edit
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            try
            {
                bNew = false;
                tsbNew.Enabled = false;
                tsbEdit.Enabled = false;
                clubsComboBox.Enabled = false;
                tsbCancel.Enabled = true;
                tsbSave.Enabled = true;

                DataRowView drv = (DataRowView)competitorsDataGridView.CurrentRow.DataBoundItem;
                this.drActiveCompetitor = (aDbDataSet.CompetitorsRow)drv.Row;

                gbList.Enabled = false;
                gbDetails.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Pretekári - editovať", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Cancel
        private void tsbCancel_Click(object sender, EventArgs e)
        {
            tsbNew.Enabled = true;
            tsbEdit.Enabled = true;
            clubsComboBox.Enabled = true;
            tsbCancel.Enabled = false;
            tsbSave.Enabled = false;

            gbList.Enabled = true;
            gbDetails.Enabled = false;

            competitorsDataGridView_SelectionChanged(null, null);
        }

        // Save
        private void tsbSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtSurname.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Zadajte priezvisko!", "Pretekári - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                if (txtFirstname.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Zadajte meno!", "Pretekári - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                if (txtSex.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Zadajte pohlavie!", "Pretekári - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                this.drActiveCompetitor["Active"] = chkActive.Checked;
                this.drActiveCompetitor["Surname"] = txtSurname.Text;
                this.drActiveCompetitor["Firstname"] = txtFirstname.Text;
                this.drActiveCompetitor["BirthDate"] = dtpBirthDate.Value;
                this.drActiveCompetitor["Sex"] = txtSex.Text;
                this.drActiveCompetitor["ClubId"] = clubsComboBox1.SelectedValue;
                this.drActiveCompetitor["CountryId"] = countriesComboBox.SelectedValue;
                this.drActiveCompetitor["Modified"] = DateTime.Now;

                if (bNew)
                {
                    aDbDataSet.Competitors.Rows.Add(this.drActiveCompetitor);
                    this.competitorsTableAdapter.Update(aDbDataSet.Competitors);
                }
                else
                {
                    this.competitorsTableAdapter.Update(this.drActiveCompetitor);
                }

                DatabaseHelper.FillCompetitorsByClubId(this.aDbDataSet.Competitors, clubsComboBox.SelectedValue.ToString());
                //this.competitorsTableAdapter.FillByClubId(this.aDbDataSet.Competitors, clubsComboBox.SelectedValue.ToString());

                tsbNew.Enabled = true;
                tsbEdit.Enabled = true;
                clubsComboBox.Enabled = true;
                tsbCancel.Enabled = false;
                tsbSave.Enabled = false;

                gbDetails.Enabled = false;
                gbList.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Pretekári - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Close
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsbImport_Click(object sender, EventArgs e)
        {
            try
            {
                string fileName = string.Empty;

                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "CSV súbory (*.csv)|*.csv";
                dlg.Title = "Vyberte CSV súbor na import";

                if (dlg.ShowDialog() == DialogResult.OK)
                    fileName = dlg.FileName;

                if (string.IsNullOrEmpty(fileName))
                    return; // user didn't select a file to open

                Cursor.Current = Cursors.WaitCursor;

                // Import from file
                RegistrationImportHelper.ImportCompetitors(fileName);

                // Reload
                DatabaseHelper.FillCompetitorsByClubId(this.aDbDataSet.Competitors, clubsComboBox.SelectedValue.ToString());

                MessageBox.Show("Import pretekárov prebehol úspešne.", "Import pretekárov", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }



    }
}
